
using System.Diagnostics;
using log4net;

//[assembly: log4net.Config.XmlConfigurator(ConfigFile="log.config",Watch=true)]
namespace logger{
    class OneThread{
        private long cnt_max_ = 0;
        private long tc_max_=0;
        private long tc_min_=9999;
        private long tc_all_real_=0;
        private long tc_all_=0;
        private long tc_5_=0;  // 1~5
        private long tc_10_=0; // 5~10
        private long tc_20_=0;  // 10~20
        private long tc_50_=0;  // 20~50
        private long tc_g50_=0;

        private long[] data_arr;
        
        public void test()
        {
            Stopwatch sw = new Stopwatch();
            Stopwatch sw_all = Stopwatch.StartNew();
            cnt_max_ = 600000;
            data_arr = new long[cnt_max_];

            for(int i=0; i<cnt_max_; i++){
                sw.Restart();
                CANBoardLog.writeLog("error messageeeeeeeeeeeeeeeeee "+i);
                sw.Stop();
                data_arr[i] = sw.ElapsedMilliseconds;
                update(sw);
                // Tip Message
                if (i%10000==0){
                    System.Console.WriteLine("Process {0}/{1}",i,cnt_max_);
                }
            }
            sw_all.Stop();
            tc_all_real_ = sw_all.ElapsedMilliseconds;
            show();
        }

        private void update(Stopwatch sw){
            long cur_tc = sw.ElapsedMilliseconds;
            tc_all_+=cur_tc;

            if( tc_max_<cur_tc) tc_max_ = cur_tc;
            if(tc_min_>cur_tc) tc_min_ = cur_tc;
            if(cur_tc>50) tc_g50_++;    // 50+
            else if(cur_tc>20) tc_50_++; // 20~50
            else if(cur_tc>10) tc_20_++;  // 10~20
            else if(cur_tc>5) tc_10_++;   // 5~10   
            else if(cur_tc>1)  tc_5_++;  // 1~5
        }

        private void show(){
            string summary = string.Format("Count(All) = {0}\r\n"
                                        +"Time Consumption(Real All) = {1}\r\n"
                                        +"Time Consumption(All) = {2}\r\n"
                                        +"Time Consumption(Max) = {3}\r\n"
                                        +"Time Consumption(Min) = {4}\r\n"
                                        +"Time Consumption(1,5) = {5}\r\n"
                                        +"Time Consumption(5,10) = {6}\r\n"
                                        +"Time Consumption(10,20) = {7}\r\n"
                                        +"Time Consumption(20,50) = {8}\r\n"
                                        +"Time Consumption(50+) = {9}\r\n"
                                        ,cnt_max_,tc_all_real_,tc_all_,tc_max_,tc_min_
                                        ,tc_5_,tc_10_,tc_20_,tc_50_,tc_g50_);
            System.Console.WriteLine(summary);
            // Show Detail
            int show_step = 40;
            for(int i=0; i<cnt_max_+show_step; i+=show_step){
                string line = i+">"; 
                for(int j=i; j<i+show_step && j<cnt_max_; j++){
                    line=line+" "+data_arr[j];
                }
                System.Console.WriteLine(line);
                System.Console.ReadKey();
            }
        }
    }
}